<template>
	<view>
		<div class="charcontiner">
			<uni-popup type="bottom" ref="share" maskClick="false">

				<view class="top-wrap">
					<view class="banner-wrap">
						<swiper class="swiper" circular :interval="3000" :duration="1000" @change="changeSwiper">
							<swiper-item v-for="(item,index) in swiperList" :key="index">
								<view class="swiper-item">
									<image :src="item.img" mode="aspectFill"></image>
								</view>
							</swiper-item>
						</swiper>
						<view class="swiper-dots">
							<view class="swiper-dot" :class="{active: curentIndex === index}" v-for="(item,index) in swiperList" :key="index"></view>
						</view>
					</view>

					<view class="info-wrap">
						<view class="info">
							<view class="avatar">
								<image :src="info.customer.img" mode=""></image>
							</view>
							<view class="name-wrap">
								<view class="name">{{info.customer.name}}</view>
								<view class="wel">邀您一起加入</view>
								<view class="wel">去嗨鸭</view>
							</view>
						</view>
						<view class="code-wrap">
							<view class="code">
								<image :src="info.qrCode || 'http://temp.im/88x88'" mode=""></image>
							</view>
							<text class="txt">长按识别</text>
						</view>
					</view>
				</view>

				<view class="share-wrap">
					<!-- #ifdef MP-WEIXIN -->
					<view class="shares">
					<!-- #endif -->
					<!-- #ifdef H5 -->
					<view class="shares" style="justify-content: center;">
					<!-- #endif -->
						<view class="share" @click="handleSavePhoto">
							<image class="icon" src="../../static/imgs/other/camera.png" mode=""></image>
							<text>保存到相册</text>
						</view>
						<!-- #ifdef MP-WEIXIN -->
						<view class="share">
							<image class="icon" src="../../static/imgs/other/wechat.png" mode=""></image>
							<text>分享到好友</text>
							<button class="sharebutton" type="default" open-type="share"></button>
						</view>
						<!-- #endif -->
					</view>
					<view class="btn" @click="hide">取消</view>
				</view>
			</uni-popup>

			<div class="poster">
				<SharePoster v-if="drawPoster" :canvasInfo="canvasInfo" @reset="handleResetShare"></SharePoster>
			</div>
		</div>
	</view>
</template>

<script>
	import SharePoster from './sharePoster.vue'
	export default {
		components: {
			SharePoster
		},
		props: {
			swiperList: {
				type: Array,
				default: function() {
					return []
				}
			},
			info: {
				type: Object,
				default: function() {
					return {
						avatar: '',
						nickname: '去嗨鸭',
						code: '',
					}
				}
			},
			shareParam: {
				type: Object,
				default: function() {
					return {}
				}
			}
		},
		data() {
			return {
				curentIndex: 0,
				drawPoster: false,
				canvasInfo: {
					curBannerUrl: '',
					codeUrl: '',
					avatar: '',
					name: '',
					desc1: '邀您一起加入',
					desc2: '去嗨鸭',
				},
				isTrue: false, // 是否准备好
			};
		},
		watch: {
			info(v) {
				this.canvasInfo.name = v.customer.name
				this.changePath(v.customer.img, 'avatar')
				this.changePath(v.qrCode, 'codeUrl')
				// this.changePath(this.swiperList[this.curentIndex].img, 'curBannerUrl')

				// this.canvasInfo.avatar = v.avatar
				// this.canvasInfo.codeUrl = v.code
				// this.canvasInfo.curBannerUrl = this.swiperList[this.curentIndex]
			},
			// curentIndex() {
			// 	this.changePath(this.swiperList[this.curentIndex].img, 'curBannerUrl')
			// }
		},
		onShareAppMessage(res) {
			// 请在父级页面写分享的return
		},
		methods: {
			handleSavePhoto() {
				if(this.isTrue && this.canvasInfo.curBannerUrl){
					this.drawPoster = true
				}else {
					uni.showToast({
						title: '背景准备中...',
						icon: 'none'
					})
				}
			},
			changeSwiper(e) {
				this.curentIndex = e.detail.current
				this.isTrue = false
				setTimeout(() => {
					this.changePath(this.swiperList[this.curentIndex].img, 'curBannerUrl')
				}, 10)
			},
			show() {
				this.$refs.share.open()
				setTimeout(() => {
					this.changePath(this.swiperList[0].img, 'curBannerUrl')
				}, 10)
			},
			hide() {
				this.$refs.share.close()
				this.drawPoster = false
			},
			handleResetShare() {
				this.drawPoster = false
			},
			// 网络路径转临时路径
			changePath(url, name) {
				uni.getImageInfo({
					src: url,
					success: ({
						path
					}) => { // 图片线上地址转本地缓存地址，方便canvas画图
						// this.canvasInfo[name] = path
						this.$set(this.canvasInfo, name , path)
						// console.log('1337484884',this.canvasInfo[name]);
						this.isTrue = true
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.charcontiner {
		position: relative;
	}

	.top-wrap {
		border-radius: 16rpx;
		overflow: hidden;
	}

	.banner-wrap {
		height: 660rpx;
		width: 610rpx;
		margin: 0 auto;
		background-color: #fff;
		overflow: hidden;
		position: relative;
		border-radius: 16rpx 16rpx 0px 0px;

		.swiper-dots {
			position: absolute;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			z-index: 2;

			.swiper-dot {
				width: 32rpx;
				height: 6rpx;
				border-radius: 3rpx;
				background-color: #ccc;
				float: left;
				margin-right: 10rpx;

				&:last-child {
					margin-right: 0;
				}

				&.active {
					background-color: #fff;
				}
			}
		}
	}

	.info-wrap {
		width: 610rpx;
		height: 210rpx;
		margin: 0 auto 31rpx;
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 0rpx 0rpx 16rpx 16rpx;

		.info {
			display: flex;

			.avatar {
				height: 88rpx;
				width: 88rpx;
				border-radius: 50%;
				overflow: hidden;
				margin-right: 20rpx;
			}

			.name-wrap {
				display: flex;
				flex-direction: column;

				.name {
					font-size: 26rpx;
					font-weight: 500;
					margin-bottom: 18rpx;
				}

				.wel {
					font-size: 22rpx;
					color: #999;
					line-height: 30rpx;
				}
			}
		}

		.code-wrap {
			display: flex;
			flex-direction: column;
			align-items: center;

			.code {
				width: 107rpx;
				height: 107rpx;
			}

			.txt {
				font-size: 22rpx;
				color: #999;
				line-height: 36rpx;
			}
		}
	}

	.share-wrap {
		height: 310rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		border-radius: 12rpx 12rpx 0px 0px;
		background-color: #fff;

		.shares {
			flex: 1;
			padding: 0 170rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.share {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 26rpx;
				position: relative;

				.icon {
					height: 88rpx;
					width: 88rpx;
					border-radius: 50%;
					margin-bottom: 20rpx;
				}

				.sharebutton {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					background-color: transparent;

					&::after {
						border: none;
					}
				}
			}
		}

		.btn {
			height: 96rpx;
			line-height: 96rpx;
			text-align: center;
			color: #666;
			font-size: 32rpx;
			border-top: 1px solid #EBEBEB;
		}
	}

	.poster {
		height: 0;
		overflow: hidden;
		position: absolute;
		z-index: -9;
		left: -9999rpx;
	}
</style>
